package com.example.common.utils;


import com.alibaba.fastjson2.JSON;
import com.example.common.redis.RedisCache;
import com.example.demo.Controller.CityController;
import com.example.demo.Controller.RecommendController;
import com.example.demo.domain.*;
import com.example.demo.domain.DTO.ProvinceHot;
import com.example.demo.domain.DTO.SpotAvgFiveInfo;
import com.example.demo.domain.DTO.SpotMonthInfo;
import com.example.demo.domain.DTO.SpotSearch;
import com.example.demo.service.CityService;
import com.example.demo.service.ProvinceInfoService;
import com.example.demo.service.RecommendService;
import com.example.demo.service.SpotInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class SpringBootINIT {
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private CityService cityService;

    @Autowired
    private ProvinceInfoService provinceInfoService;

    @Autowired
    private SpotInfoService spotInfoService;

    @Autowired
    private CityController cityController;

    @Autowired
    private RecommendService recommendService;

    public void test() {
        AjaxResult ajaxResult = cityController.cityList();
        System.out.println(ajaxResult);
    }

    //预缓存
//    @PostConstruct
    public void springbootInit() {
//        热门城市(x为省份 y为城市 z为热度值)
        HashMap<String, HashMap<String, Float>> CityHotInfoMap = cityService.selectCityHot();
        //存入redis
        redisCache.setCacheJSONObject("CityHotInfo", CityHotInfoMap, 1440, TimeUnit.MINUTES);
        //所有城市
        List<City> cities = cityService.selectAll();
        //存入redis
        redisCache.setCacheJSONObject("cityList", cities, 1440, TimeUnit.MINUTES);

//        省份城市景区列表
        HashMap<String, HashMap<String, List<String>>> provinceCitySpot = cityService.selectProvinceCitySpot();
        //存入redis
        redisCache.setCacheJSONObject("provinceCityList", provinceCitySpot, 1440, TimeUnit.MINUTES);
        //热门省份
        List<ProvinceHot> provinceHots = provinceInfoService.selectProvinceHot();
        //存入redis
        redisCache.setCacheJSONObject("hotTop", provinceHots, 1440, TimeUnit.MINUTES);
        //省份列表
        List<ProvinceInfo> provinceInfos = provinceInfoService.selectProvinceInfo();
        //存入redis
        redisCache.setCacheJSONObject("provinceList", provinceInfos, 1440, TimeUnit.MINUTES);
        //热度前十的景点
        List<SpotHotInfo> spotHotInfos = spotInfoService.selectHotInfoTop10();
        //存入redis
        redisCache.setCacheJSONObject("spotHotTop5", spotHotInfos, 1440, TimeUnit.MINUTES);
        //推荐指数前十的景点
        List<SpotInfo> spotInfos = spotInfoService.selectRatingTop10();
        //存入redis
        redisCache.setCacheJSONObject("ratingTop10", spotInfos, 1440, TimeUnit.MINUTES);
        //星评前十
        List<SpotGradeInfo> spotGradeInfos = spotInfoService.selectGradeTop10();
        //存入redis
        redisCache.setCacheJSONObject("spotGradeTop5", JSON.toJSONString(spotGradeInfos), 1440, TimeUnit.MINUTES);
        //评论最多的景点
        SpotInfo spotGradeInMaxFos = spotInfoService.selectMaxSum();
        //存入redis
        redisCache.setCacheJSONObject("maxSumSpot", spotGradeInMaxFos, 1440, TimeUnit.MINUTES);
//        季节最热门景区(x轴 季节 y轴 景区  z轴 该季度总评论数)
        HashMap<String, HashMap<String, Object>> seasonSpotInfo = spotInfoService.selectSeasonSpotInfo();
        //存入redis
        redisCache.setCacheJSONObject("seasonSpot", seasonSpotInfo, 1440, TimeUnit.MINUTES);
        //景区搜索栏
        List<SpotSearch> spotSearches = spotInfoService.selectSpotOtherInfo();
        //存入redis
        redisCache.setCacheJSONObject("spotInfoSearch", spotSearches, 1440, TimeUnit.MINUTES);
        //各景区评论数
        List<SpotComCntInfo> spotComCntInfos = spotInfoService.selectSpotComCntInfo();
        //存入redis
        redisCache.setCacheJSONObject("spotComInfo", spotComCntInfos, 1440, TimeUnit.MINUTES);
        //全部各项景点的平均指标
        SpotAvgFiveInfo spotAvgFiveInfo = spotInfoService.selectAllSpotAvgFiveInfo();
        //存入redis
        redisCache.setCacheJSONObject("spotAllFiveInfo", spotAvgFiveInfo, 1440, TimeUnit.MINUTES);
        //推荐指数前十的景点评论数
        HashMap<String, HashMap<String, Object>> selectRatingTop10CntInfo = spotInfoService.selectRatingTop10CntInfo();
        //存入redis
        redisCache.setCacheJSONObject("ratingTop10CntInfo", selectRatingTop10CntInfo, 1440, TimeUnit.MINUTES);
//rating前十景区的各省的人数
        HashMap<String, HashMap<String, Object>> result = spotInfoService.selectRatingTop10ProvinceInfo();
        //存入redis
        redisCache.setCacheJSONObject("selectRatingTop10ProvinceInfo", result, 30, TimeUnit.MINUTES);
        //各月景点评论和平均口碑
        HashMap<String, List<SpotMonthInfo>> spotMonthInfoListHashMap = spotInfoService.selectSpotMonthInfo();
        //存入redis
        redisCache.setCacheJSONObject("spotMonthInfoList", spotMonthInfoListHashMap, 30, TimeUnit.MINUTES);
        //城市景点个数
        List<CitySpotNum> cityPeople = cityService.selectCitySpotNum();
        //存入redis
        redisCache.setCacheJSONObject("peopleCityList", cityPeople, 1440, TimeUnit.MINUTES);
        //城市景点总数平均值
        List<CitySpotCountAndRating> citySpotCountAndRatings = cityService.selectCitySpotAvgRating();
        //存入redis
        redisCache.setCacheJSONObject("citySpotAvgRating", citySpotCountAndRatings, 1440, TimeUnit.MINUTES);
        //景区等级
        List<SpotLevelCnt> spotLevelCnts = spotInfoService.selectSpotLevelCnt();
        //设置缓存一天
        redisCache.setCacheJSONObject("SpotLevelCnt", spotLevelCnts, 1440, TimeUnit.MINUTES);
        //推荐接口
        RecommendSearch recommendSearch = new RecommendSearch();
        recommendSearch.getProvinceList().add("湖北省");
        List<RecommendInfo> recommendInfos = recommendService.selectAIRecommendSpotInfo(recommendSearch);
        //设置缓存一天
        redisCache.setCacheJSONObject("AISpotInfoSearch", recommendInfos, 1440, TimeUnit.MINUTES);
    }
}
